﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Tron
{
    public partial class MenuUserControl : UserControl
    {
        private TronForm tronForm;

        private struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        public MenuUserControl(TronForm tronForm)
        {
            InitializeComponent();

            this.tronForm = tronForm;
            this.Size = new Size(Constantes.Xmatrice * Constantes.tall, Constantes.Xmatrice * Constantes.tall + 50);
            this.timeLabel.Location = new Point(Constantes.Xmatrice * Constantes.tall - 80, 0);
            this.timeLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte) (0)));

            Timer clockTimer = new Timer();
            clockTimer.Interval = 1000;
            clockTimer.Tick += new System.EventHandler(this.refreshTimeLabel);
            clockTimer.Start();
        }

        [DllImport("Kernel32.dll")]
        private extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

        private String getTime()
        {
            SYSTEMTIME stime = new SYSTEMTIME();
            GetLocalTime(ref stime);

            return stime.wHour.ToString() + ":" + stime.wMinute.ToString() + ":" + stime.wSecond.ToString();
        }

        private void refreshTimeLabel(object send, EventArgs e)
        {
            this.timeLabel.Text = this.getTime();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void localToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // Au lancement de la partie, on crée sa vue (gameUserControl)
            gameUserControl = new GameUserControl();
            gameUserControl.Visible = false;
            gameUserControl.Size = new Size(Constantes.Xmatrice * Constantes.tall, Constantes.Xmatrice * Constantes.tall + 50);
            gameUserControl.TimeLabel = timeLabel;
            tronForm.Controls.Add(gameUserControl);

            // On cache le menu principal et on affiche le jeu
            gameUserControl.Visible = !gameUserControl.Visible;
            this.Visible = !this.Visible;
            
            // Lancement du jeu
            gameUserControl.Menu = this;
            gameUserControl.launchNewGame(new Player("Blue", new Moto()), new Player("Yellow", new Moto()));
        }

        private void createLANServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // LanMenuControl
            lanMenuControl = new LANMenuControl();
            lanMenuControl.Size = new Size(Constantes.Xmatrice * Constantes.tall, Constantes.Ymatrice * Constantes.tall + 50);
            tronForm.Controls.Add(lanMenuControl);
            lanMenuControl.Menu = this;
            lanMenuControl.TimeLabel = timeLabel;
            this.Visible = !this.Visible;
            
            lanMenuControl.createServer();
        }

        private void joinLANServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // LanMenuControl
            lanMenuControl = new LANMenuControl();
            lanMenuControl.Size = new Size(Constantes.Xmatrice * Constantes.tall, Constantes.Ymatrice * Constantes.tall + 50);
            tronForm.Controls.Add(lanMenuControl);
            lanMenuControl.TimeLabel = timeLabel;
            this.Visible = !this.Visible;

            lanMenuControl.joinServer();
        }
    }
}
